﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Leadtools.Codecs;
using Leadtools;
using MINERP.ComControls;
using Leadtools.WinForms;
using MINERP.BLL.InnerReturn;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;

namespace MINERP.NewInerReturn.Forms
{
    public partial class FrmInnerReturn : FrmParent
    {
        RasterThumbnailBrowser rasterThumbnailBrowserPicView;
        InnerReturnBLL2 _bll;
        public FrmInnerReturn()
        {
            InitializeComponent();
            rasterThumbnailBrowserPicView = new RasterThumbnailBrowser();
            rasterThumbnailBrowserPicView.Visible = false;
            rasterThumbnailBrowserPicView.Dock = DockStyle.Fill;
            panel1.Controls.Add(rasterThumbnailBrowserPicView);

            _bll = new InnerReturnBLL2();
        }


        bool isShowDetail = true;//用于切换显示,默认是显示明细列表
        private void simpleButtonChangeDisplay_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            simpleButtonChangeDisplay.Enabled = false;

            if (isShowDetail)//将要显示图片
            {
                gridControlphotos.Visible = false;
                rasterThumbnailBrowserPicView.Visible = true;
                isShowDetail = false;
                ///显示缩略图
                rasterThumbnailBrowserPicView.Items.Clear();
                foreach (PhotoView photo in this.CurrPhotoList)
                {
                    try
                    {
                        RasterCodecs.Startup();
                        RasterCodecs codecs = new RasterCodecs();
                        CodecsThumbnailOptions options = new CodecsThumbnailOptions();
                        options.Resample = true;
                        options.LoadStamp = true;
                        options.MaintainAspectRatio = true;
                        RasterImage img;
                        try
                        {
                            img = codecs.ReadThumbnail(photo.picPath, options, 1);
                        }
                        catch
                        {
                            //为图片设置Stamp
                            RasterImage imgInfo = codecs.Load(photo.picPath);
                            Rectangle rc = RasterImage.CalculatePaintModeRectangle(imgInfo.Width, imgInfo.Height, new Rectangle(0, 0, 128, 128), RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near);
                            codecs.Options.Jpeg.Save.StampWidth = rc.Width;
                            codecs.Options.Jpeg.Save.StampHeight = rc.Height;
                            codecs.Options.Jpeg.Save.SaveWithStamp = true;
                            codecs.Options.Jpeg.Save.StampBitsPerPixel = 24;
                            codecs.Save(imgInfo, photo.picPath, RasterImageFormat.Jpeg, 0);
                            //重新读缩略图
                            img = codecs.ReadThumbnail(photo.PhotoBarCode, options, 1);
                        }
                        //int index = photoPath.LastIndexOf("\\");
                        //string fileName = photoPath.Substring(index + 1);
                        RasterImageListItem item = new RasterImageListItem(img, 1, photo.PhotoBarCode);
                        item.Tag = photo.PhotoBarCode;
                        rasterThumbnailBrowserPicView.Items.Add(item);

                    }
                    catch { this.Cursor = Cursors.Default; simpleButtonChangeDisplay.Enabled = true; return; }
                }
            }
            else//显示明细列表
            {
                gridControlphotos.Visible = true;
                rasterThumbnailBrowserPicView.Visible = false;
                isShowDetail = true;

            }
            this.Cursor = Cursors.Default;
            simpleButtonChangeDisplay.Enabled = true;

        }
        List<PhotoView> CurrPhotoList = new List<PhotoView>();
        private void simpleButtonchakan_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            List<string> displayString = new List<string>();
            foreach (PhotoView c in CurrPhotoList)
            {

                displayString.Add(c.picPath);
                displayString.Add(c.PhotoBarCode);

            }
            if (displayString.Count == 0)
            {
                MessageBox.Show("请先填写返单照片！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                try
                {
                    BigPicViewer viewer = new BigPicViewer();
                    viewer.PhotoPaths = displayString;
                    viewer.CurrentIndex = 0;
                    viewer.ShowDialog();
                }
                catch (Exception ex)
                {
                    this.Cursor = Cursors.Default;
                    MessageBox.Show("读取照片失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            this.Cursor = Cursors.Default;
        }
        public string CurrBarCode = "";
        PhotoView CurrentView = new PhotoView();
        FlowView SelectedFlow = new FlowView();

        private void textEditPhotoId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                int length = textEditPhotoId.Text.Trim().Length;
                if (length < 10)
                {
                    string NUM0 = string.Empty;
                    for (int i = 0; i < 10 - length - 1; i++)
                    {
                        NUM0 += "0";
                    }
                    textEditPhotoId.Text = "3" + NUM0 + textEditPhotoId.Text;
                }
                this.CurrBarCode = textEditPhotoId.Text.Trim();
                if (CurrPhotoList.FirstOrDefault(p => p.PhotoBarCode == CurrBarCode) != null)
                {
                    MessageBox.Show("照片已存在在列表中！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                string ERRMsg = "";
                CurrentView = _bll.GetPhotoViewByBarCode(CurrBarCode, out ERRMsg);

                if (CurrentView != null)
                {


                    //绑定流程和人员
                    //comboBoxErrUser.DataSource = view.ListFlow;
                    //comboBoxErrUser.DisplayMember = "UserName";
                    //comboBoxErrUser.ValueMember = "UserID";
                    labelControlPhotoName.Text = CurrentView.PhotoName;
                    labelControlProductName.Text = CurrentView.OrderProductName;
                    comboBoxErrFlow.DataSource = CurrentView.ListFlow;
                    comboBoxErrFlow.DisplayMember = "TechName";
                    comboBoxErrFlow.ValueMember = "TechId";

                    //if (SelectedFlow != null && SelectedFlow.Tech != null && SelectedFlow.UserId != 0)
                    //{
                    //    for (int i = 0; i < comboBoxErrFlow.Items.Count; i++)
                    //    {
                    //        if (((FlowView)comboBoxErrFlow.Items[i]).UserId == SelectedFlow.UserId)
                    //        {
                    //            comboBoxErrFlow.SelectedIndex = i;
                    //            break;
                    //        }
                    //    }
                    //}
                    //else
                    //{
                        for (int i = 0; i < comboBoxErrFlow.Items.Count; i++)
                        {
                            if (((FlowView)comboBoxErrFlow.Items[i]).Tech.TechType == (int)TechType.KuoYin || ((FlowView)comboBoxErrFlow.Items[i]).Tech.TechType == (int)TechType.DigitalOutput)
                            {
                                comboBoxErrFlow.SelectedIndex = i;
                                break;
                            }
                        }
                    //}
                }
                else
                {
                    MessageBox.Show(ERRMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
        }

        private void FrmInnerReturn_Load(object sender, EventArgs e)
        {
            comboBoxErrUser.DataSource = _bll.GetAllUsers();
            comboBoxErrUser.DisplayMember = "UserName";
            comboBoxErrUser.ValueMember = "UserID";
            comboBoxErrFlow.DataSource = _bll.GetAllTech();
            comboBoxErrFlow.DisplayMember = "TechName";
            comboBoxErrFlow.ValueMember = "TechId";
            foreach (string str in _bll.GetAllPaperWasteType())
            {
                comboBoxRemark.Items.Add(str);

            }
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (CurrPhotoList.Count() > 0)
                {
                    Dictionary<int, int> outDict = new Dictionary<int, int>();
                    this.Cursor = Cursors.WaitCursor;
                    List<PhotoView> rtnList = _bll.CreateInnerReturn(CurrPhotoList, out outDict);
                    if (rtnList.Count() == 0)
                    {
                        MessageBox.Show("全部照片返工完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        gridControlphotos.DataSource = null;
                        gridControlphotos.RefreshDataSource();
                    }
                    else
                    {
                        gridControlphotos.DataSource = rtnList;
                        gridControlphotos.RefreshDataSource();
                        MessageBox.Show("部分照片返工完成，请产看列表中备注的错误信息！", "提示", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                    FrmShowPaperCode from = new FrmShowPaperCode(outDict);
                    from.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请输入返单照片！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                intCurrTechid = 0;
                intCurrUserId = 0;
                CurrPhotoList = new List<PhotoView>();
                textEditPhotoId.Focus();
            }
            catch (Exception exxx)
            {

                MessageBox.Show(exxx.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
               
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private int intCurrTechid = 0;
        private int intCurrUserId = 0;
        private void comboBoxErrUser_DropDownClosed(object sender, EventArgs e)
        {
            // intCurrUserId = ((User_Info)comboBoxErrUser.SelectedItem).UserID;
        }

        private void comboBoxErrFlow_DropDownClosed(object sender, EventArgs e)
        {
            //  intCurrTechid = ((Tb_Tech_Info)comboBoxErrFlow.SelectedItem).TechId;
            if (((FlowView)comboBoxErrFlow.SelectedItem) != null)
                SelectedFlow = ((FlowView)comboBoxErrFlow.SelectedItem);
        }

        private void simpleButtonCancel_Click(object sender, EventArgs e)
        {
            //intCurrTechid = 0;
            //intCurrUserId = 0;
            //gridControlphotos.DataSource = null;
            //gridControlphotos.RefreshDataSource();
            //CurrPhotoList = new List<PhotoView>();

            int[] selectRowsID = gridViewPhotos.GetSelectedRows();
            foreach (int rowsId in selectRowsID)
            {
                PhotoView view = (PhotoView)gridViewPhotos.GetRow(rowsId);
                CurrPhotoList.Remove(view);
            }
            gridControlphotos.RefreshDataSource();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButtonAdd_Click(object sender, EventArgs e)
        {
            if (CurrentView == null)
            {
                MessageBox.Show("照片重复添加或者未输入照片！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (CurrPhotoList.FirstOrDefault(p => p.PhotoId == CurrentView.PhotoId) == null)
            {
                CurrentView.DutyMan = ((User_Info)comboBoxErrUser.SelectedItem);
                CurrentView.CreateMan = new User_Info() { UserID = base.userId, UserName = base.userName };//((User_Info)comboBoxErrUser.SelectedItem);//测试 实际去主界面传入值
                CurrentView.TechTo = ((FlowView)comboBoxErrFlow.SelectedItem).Tech;
                CurrentView.Remark = comboBoxRemark.Text;
                CurrPhotoList.Add(CurrentView);
                gridControlphotos.DataSource = CurrPhotoList;
                gridControlphotos.RefreshDataSource();
                CurrentView = null;
                textEditPhotoId.Text = "";
                labelControlPhotoName.Text = "";
                labelControlProductName.Text = "";
            }
            else
            {
                MessageBox.Show("照片已存在在列表中！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void comboBoxErrFlow_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (comboBoxErrFlow.SelectedItem != null)
            {
                FlowView view = (FlowView)comboBoxErrFlow.SelectedItem;
                int dutytechid = view.TechId;

                int dutyUserid = view.UserId;
                if (dutyUserid != 0)
                {
                    //comboBoxErrUser.Enabled = false;
                    for (int i = 0; i < comboBoxErrUser.Items.Count; i++)
                    {
                        if (((User_Info)comboBoxErrUser.Items[i]).UserID == dutyUserid)
                        {
                            comboBoxErrUser.SelectedIndex = i;
                            break;
                        }
                    }
                }

            }
        }


    }
}
